package com.hz.zbl.controller;

import com.hz.zbl.model.enums.UserTypeEnum;
import com.hz.zbl.model.request.ApiResponse;
import com.hz.zbl.service.UserManagerService;
import org.apache.ibatis.annotations.Param;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/user-manager")
public class UserManagerController {

    @Resource
    UserManagerService userManagerService2;


    //添加年级
    @PostMapping("/add-grade")
    public String addGrade(@RequestParam("grade_name") String grade_name,
                           @RequestParam("grade_year") String grade_year){

        int add = userManagerService2.addGrade(grade_name,grade_year);

        return add > 0 ? ApiResponse.ok(add) : ApiResponse.intError();
    }


    //添加班级
    @PostMapping("/add-class")
    public String addClass(@RequestParam("class_name") String class_name,
                           @RequestParam("grade_id") int grade_id){

        int add = userManagerService2.addClass(class_name,grade_id);

        return add > 0 ? ApiResponse.ok(add) : ApiResponse.intError();
    }

    //添加用户
    @PostMapping("/add-user")
    public String addUser(@RequestParam("username") String username,
                          @RequestParam("realname") String realname,
                          @RequestParam("password") String password,
                          @RequestParam(value = "student_id", required = false) Integer student_id,
                          @RequestParam(value = "teacher_id", required = false) Integer teacher_id,
                          @RequestParam(value = "class_id", required = false) Integer class_id,
                          @RequestParam(value = "grade_id", required = false) Integer grade_id,
                          @RequestParam("user_type") int user_type) {
        int result = 0;
        if (user_type == UserTypeEnum.MANAGER.getUser_type()) {
            result = userManagerService2.addManager(username, realname, password);
        } else if (user_type == UserTypeEnum.TEACHER.getUser_type()) {
            result = userManagerService2.addTeacher(username, realname, password, teacher_id);
        } else if (user_type == UserTypeEnum.STUDENT.getUser_type()) {
            result = userManagerService2.addStudent(username, realname, password, student_id, class_id, grade_id);
        }
        return result > 0 ? ApiResponse.ok(result) : ApiResponse.intError();


    }
}
